﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLK.Database;
using QLK.Web.App_Code;

namespace QLK.Web
{
    public partial class SupplierManagement : System.Web.UI.Page
    {
        //public int _iStt = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            GridSupplier.AllowPaging = true;
            GridSupplier.PageSize = 4;
            if (!IsPostBack)
            {
                LoadAllSuppliers();
            }
        }
        int i = 1;
        public int Get_STT()
        {
            return i++;   // mỗi dòng cho lên là 1 
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(PageUrls.SupplierDetails);
        }

        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            SupplierBLL suppBLL = new SupplierBLL();
            Supplier supp=new Supplier();
            ImageButton obj = sender as ImageButton;
            supp.SupplierID = int.Parse(obj.CommandArgument.ToString());
            //try
            //{

            suppBLL.DeleteSupplierBySupplierID(supp);
            LoadAllSuppliers();
        }

        protected void imgEdit_Click(object sender, ImageClickEventArgs e)
        {
            //Get gridview index
            GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
            int index = gvRow.RowIndex;

            Supplier supp = new Supplier();
            supp.SupplierName = GridSupplier.Rows[index].Cells[1].Text.ToString();
            supp.Address = GridSupplier.Rows[index].Cells[2].Text.ToString();
            supp.PhoneNumber = GridSupplier.Rows[index].Cells[3].Text.ToString();
            ImageButton img = sender as ImageButton;
            Response.Redirect(string.Format("{0}?{1}={2}&{3}={4}&{5}={6}&{7}={8}", PageUrls.SupplierDetails,
                Columns.SupplierID,img.CommandArgument,Columns.SupplierName,supp.SupplierName,
                Columns.Address,supp.Address,Columns.PhoneNumber,supp.PhoneNumber));
        }

        private void LoadAllSuppliers()
        {
            SupplierBLL bll = new SupplierBLL();
            GridSupplier.DataSource = bll.GetAllSupplier();
            GridSupplier.DataBind();
        }

        protected void GridSupplier_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridSupplier.PageIndex = e.NewPageIndex;
            int trang_thu = e.NewPageIndex;
            int so_dong = GridSupplier.PageSize;

            i = trang_thu * so_dong + 1;
            LoadAllSuppliers();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string suppname;
            foreach (GridViewRow row in GridSupplier.Rows)
            {
                CheckBox checkbox = (CheckBox)row.FindControl("chkSelect");
                if (checkbox.Checked)
                {
                    suppname = GridSupplier.DataKeys[row.RowIndex].Value.ToString();
                    Supplier supp = new Supplier();
                    supp.SupplierName = suppname;
                    try
                    {
                        SupplierBLL suppBLL = new SupplierBLL();
                        suppBLL.DeleteSupplierBySupplierID(supp);
                    }
                    catch (Exception ex)
                    {
                        Response.Write("<script>alert('Lỗi :" + ex.Message + "')</script>");
                    }
                }
            }
            LoadAllSuppliers();
            
        }
    }
}
